package com.example.demo.controller;

import com.example.demo.entity.Music;

import com.example.demo.entity.MusicQuery;
import com.example.demo.entity.TypeCount;
import com.example.demo.service.MusicService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.demo.util.FileUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("music")
public class MusicController {



    ////首页热门歌曲展示
    @GetMapping("/sorted-by-duration")
    public List<Music> getMusicSortedByDuration() {
        return musicService.findMusicByDuration();
    }

    @Autowired
    private MusicService musicService;
    //跟新播放次数
    @PostMapping("/incrementPlayCount")
    public ResponseEntity<?> incrementPlayCount(@RequestBody Map<String, String> payload) {
        String musicId = payload.get("music_id");
        boolean success = musicService.incrementPlayCount(musicId);
        if (success) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }






//    @GetMapping("/g1")
//    public PageInfo<Music> getPagesMusic(
//            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
//            @RequestParam(name = "size", defaultValue = "8") Integer pageSize) {
//        System.out.println("Page Number: " + pageNum + ", Page Size: " + pageSize); // 调试输出
//        return musicService.findMusicPage(pageNum, pageSize);
//    }


    //按条件分页无bug查询
    @GetMapping("/search")
    public PageInfo<Music> searchMusics(
            @RequestParam Map<String, String> allParams,
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "size", defaultValue = "8") Integer pageSize) {
        System.out.println("Received pageNum: " + pageNum + ", pageSize: " + pageSize+allParams); // 打印参数值

        MusicQuery query = buildQuery(allParams);
        return musicService.searchMusics(query, pageNum, pageSize);
    }

    private MusicQuery buildQuery(Map<String, String> allParams) {
        MusicQuery query = new MusicQuery();
        query.setMusic_name(allParams.getOrDefault("music_name", null));
        query.setArtist(allParams.getOrDefault("artist", null));
        query.setType(allParams.getOrDefault("type", null));
        return query;
    }



    // 获取按热度排序的歌曲
    @GetMapping("/g1/popularity")
    public List<Music> getMusicByPopularity() {
        return musicService.findMusicByPopularity();
    }
    @GetMapping("/g1/type")
    public List<Music> getMusicByType(String type) {
        return musicService.findMusicAll();
    }

    @PostMapping("/upload")
    public String getPicFileName(MultipartFile file){
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));

        String filePath = FileUtil.getUploadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis()+typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }




    @GetMapping("/g1/{id}")
    public Music getMusicBYID(@PathVariable Integer id){
        return musicService.findMusicID(id);
    }

    //批量获取歌曲信息
    @GetMapping("/g1/ids")
    public List<Music> getMusicsByIDs(@RequestParam("ids") List<Integer> ids) {
        return musicService.findMusicsByIDs(ids);
    }



    @PostMapping("/hhl")
    public Integer addMusic(@RequestBody Music music){
        System.out.println(music);
        return musicService.insertMusic(music);
    }

    @PutMapping("/u1")
    public  Integer updateMusic(@RequestBody  Music music){

        System.out.println(music);
        return musicService.updateMusic(music);
    }

    @DeleteMapping("/d1/{id}")
    public  Integer deleteMusic(@PathVariable("id") Integer music_id ){
        return  musicService.deleteMusic(music_id );
    }
    @GetMapping("/type-proportions")
    public ResponseEntity<List<TypeCount>> getMusicTypeProportions() {
        List<TypeCount> proportions = musicService.getMusicTypeProportions();
        return ResponseEntity.ok(proportions);
    }

}

